package demoserver;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author user
 */
public class Server {
//    private String host = "sl7.sunkhoai.com";
    private int port = 9999;
    private boolean running = true;
    protected ServerSocket serverSocket;
    private long delay = 150;
    int idClient;
    int countRoom;
    
    
       
    public static final int REQUEST_USER = 101;
    public static final int REQUEST_NOTE = 102;
    public static final int REQUEST_TEXT = 103;   
    public static final int REQUEST_ICON = 104;    
    public static final int REQUEST_IMAGE = 105;
    public static final int REQUEST_LIST = 106;
    public static final String END_LIST = "\\\\\\";
    public static final int REQUEST_END = -1;
    public static final int REQUEST_MANGA = 107;
    
    public static final int REQUEST_END_LIST = 108;
    public static final int REQUEST_END_READ = 109;
    public static final int REQUEST_END_SYSTEM = 110;
    public static final int REQUEST_ERROR_ICON = 111;
    public static final int REQUEST_ERROR_IMG = 112;
    public static final int SEND_ADDRESS=210;
    public static final int SEND_SUBJ=211;
    public static final int SEND_CONTENT=212;
    public static final int SEND_ACTION=213;
    public static final byte TEXT = 1;
    public static final byte IMAGE = 2;
    public static final byte ICON = 3; 
    public static final byte NOTE = 4;

    public Server(){
        try {

            System.out.println("server run");
            serverSocket = new ServerSocket(port);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void start(){
            acceptConnections();
    }
    

    
    
    public void close(){
        running = false;
        try {
            serverSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void acceptConnections() {
        while (running) {
            long startTime = System.currentTimeMillis();
                Socket socket;
                try {
                    socket = serverSocket.accept();
                    socket.setTcpNoDelay(true);
                    //Create Client here
                    idClient++;
                    System.out.println("connected id "+idClient);
                    Connector client = new Connector(socket,this);
                } catch (IOException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
                long timeTake = System.currentTimeMillis() - startTime;
                if(timeTake < delay){
                    synchronized(this){
                        try {
                            wait(delay - timeTake);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
                else {
                        Thread.currentThread().yield();
                }
        }
    }

}



